﻿using AppMVVM.ViewModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace AppMVVM.Views
{
    /// <summary>
    /// Interaction logic for ConnectionView.xaml
    /// </summary>
    public partial class ConnectionView : UserControl
    {
        public ConnectionView()
        {
            InitializeComponent();
        }
        private void OnPasswordChanged(object sender, RoutedEventArgs e)
        {
            ((MainWindowViewModel)DataContext).Credentials.Password = ((PasswordBox)sender).Password;
        }

        private void PasswordTextBox_KeyUp(object sender, KeyEventArgs e)
        {
            //TODO: Used attached property
            MainWindowViewModel mainWindowViewModel = DataContext as MainWindowViewModel;
            if (e.Key == Key.Enter && mainWindowViewModel != null)
            {
                mainWindowViewModel.ConnectCommand.Execute(null);
                if (mainWindowViewModel.Connected)
                {
                    FeedReader reader = FeedReader.Instance;
                    var feedReaderViewModel = new FeedReaderViewModel(mainWindowViewModel.UserData);

                    reader.DataContext = feedReaderViewModel;
                    reader.Show();
                }
            }
        }

        private void ConnectButton_Click(object sender, RoutedEventArgs e)
        {
            MainWindowViewModel mainWindowViewModel = DataContext as MainWindowViewModel;
            if (mainWindowViewModel != null)
            {
                mainWindowViewModel.ConnectCommand.Execute(null);
                if (mainWindowViewModel.Connected)
                {
                    FeedReader reader = FeedReader.Instance;
                    var feedReaderViewModel = new FeedReaderViewModel(mainWindowViewModel.UserData);

                    reader.DataContext = feedReaderViewModel;
                    reader.Show();
                }
            }
        }
    }
}
